<!--[if IE]>
<meta http-equiv="X-UA-Compatible" content="IE=5,IE=9"><![endif]-->
<!DOCTYPE html>
<html>
<head>
    <title>MXGraph Editor Lite</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <link rel="stylesheet" type="text/css" href="styles/grapheditor.css">
    <script type="text/javascript">
        // Parses URL parameters. Supported parameters are:
        // - lang=xy: Specifies the language of the user interface.
        // - touch=1: Enables a touch-style user interface.
        // - storage=local: Enables HTML5 local storage.
        // - chrome=0: Chromeless mode.
        var urlParams = (function (url) {
            var result = new Object();
            var idx = url.lastIndexOf('?');

            if (idx > 0) {
                var params = url.substring(idx + 1).split('&');

                for (var i = 0; i < params.length; i++) {
                    idx = params[i].indexOf('=');

                    if (idx > 0) {
                        result[params[i].substring(0, idx)] = params[i].substring(idx + 1);
                    }
                }
            }

            return result;
        })(window.location.href);
        // Default resources are included in grapheditor resources
        mxLoadResources = false;
        mxBasePath = './mxgraph/';
    </script>
    <script type="text/javascript" src="./pre.min.js"></script>
    <script type="text/javascript" src="./mxgraph/js/mxClient.js"></script>
    <script type="text/javascript" src="./all.min.js"></script>
    <style type="text/css">
        .geEditor {
            font-size: 12px;
        }
    </style>
</head>
<body class="geEditor">
<script type="text/javascript">
    // Extends EditorUi to update I/O action states based on availability of backend
    var editor = null;
    window.editorInit = function (success, error) {
        console.log('graphEditor.initEditor');
        if (!editor) {
            setTimeout(function () {
                window.editorInit();
            }, 100);
            return;
        }
        editor.graph.model.addListener('change', function (e) {
            console.log('graphEditor.editor.content.change');
            var xmlSerializer = new XMLSerializer();
            window.parent.postMessage({
                type: 'ContentChange',
                data: xmlSerializer.serializeToString(editor.getGraphXml())
            }, '*');
        });
        success && success();
    };
    window.editorGetContent = function (success, error) {
        console.log('graphEditor.getEditorContent');
        if (!editor) {
            return null;
        }
        var xmlSerializer = new XMLSerializer();
        success(xmlSerializer.serializeToString(editor.getGraphXml()))
    };
    window.editorSetContent = function (success, error, content) {
        console.log('graphEditor.setEditorContent', content);
        if (!editor) {
            return;
        }
        var node = mxUtils.parseXml(content);
        editor.setGraphXml(node.documentElement);
        success && success();
    };
    var doGetSvg = function (cb) {
        const xmlSerializer = new XMLSerializer()
        const svg = editor.graph.getSvg(null, 1, null, null, true, null, null)
        cb(xmlSerializer.serializeToString(svg))
    }
    window.editorExport = function (success, error) {
        console.log('graphEditor.exportEditor');
        if (!editor) {
            return null;
        }
        doGetSvg(success)
    };
    (function () {
        var editorUiInit = EditorUi.prototype.init;
        EditorUi.prototype.init = function () {
            editorUiInit.apply(this, arguments);
            editor = this.editor
        };
        // Adds required resources (disables loading of fallback properties, this can only
        // be used if we know that all keys are defined in the language specific file)
        mxResources.loadDefaultBundle = false;
        var bundle = mxResources.getDefaultBundle(RESOURCE_BASE, mxLanguage) ||
            mxResources.getSpecialBundle(RESOURCE_BASE, mxLanguage);
        // Fixes possible asynchronous requests
        mxUtils.getAll([bundle, STYLE_PATH + '/default.xml'], function (xhr) {
            // Adds bundle text to resources
            mxResources.parse(xhr[0].getText());

            // Configures the default graph theme
            var themes = new Object();
            themes[Graph.prototype.defaultThemeName] = xhr[1].getDocumentElement();
            // Main
            new EditorUi(new Editor(urlParams['chrome'] == '0', themes));
        }, function () {
            document.body.innerHTML = '<center style="margin-top:10%;">Error loading resource files. Please check browser console.</center>';
        });
    })();
</script>
</body>
</html>
